---
title: "Descriptive Statistics for Our Data - Fixed Onion Prices"
author: "Digvijay Ghotane"
date: "5/20/2020"
output: pdf_document
header-includes:
  - \usepackage{dcolumn}
  - \usepackage{rotating, graphicx}
  - \maxdeadcycles=200
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, results = 'hide')
```
```{r echo = TRUE, message = F, warning=F}
require(tidyverse)
require(stargazer)
require(lubridate)
require(zoo)
library(mfx)
require(psych)
library(skimr)
```

```{r message = F}
final = read_csv('data/output_data/final_onion_fixed.csv')
```

# 1  
```{r}
final %>% 
  group_by(district) %>% 
  summarize(stone = sum(stone)) %>% 
  arrange(desc(stone)) %>%
  mutate(district=fct_reorder(district,stone)) %>%
  mutate(color = ifelse(stone>100, 1, 0)) %>% 
  ggplot(aes(x=stone, y=district, label = stone)) + 
  geom_bar(stat="identity") +
  geom_text(color = 'black', hjust = 0, family = "Times", size = 3.5) +
  labs(x = "Number of Days Recorded for Stone Pelting Incidents",
       y = "Districts",
       title = "Number of Days of Stone Pelting Incidents by District",
       subtitle = "For the Newspaper+SATP+ACLED Dataset (From August 2013 to December 2017)") +
  theme(legend.position="none",
        plot.title = element_text(hjust = 0.5, family = "Times", face = 'bold'),
        plot.subtitle = element_text(hjust = 0.5, family = "Times", face = 'italic'),
        axis.title = element_text(hjust = 0.5, family = "Times"),
        axis.text.x = element_text(hjust = 0.5, family = "Times"),
        axis.text.y = element_text(hjust = 0.5, family = "Palatino"),
        plot.background = element_blank(),
        plot.caption = element_text(hjust = 0.5, family = "Palatino"))
```

\newpage 

# 2  
```{r echo=FALSE, results='asis', warning = F, error = F, message=F}
final %>% 
  group_by(district) %>% 
  mutate(stone = sum(stone)) %>% 
  dplyr::select(stone, MajorityReligion, UrbanVSRural, PopulationGroup) %>% 
  mutate(PopulationGroup = ifelse(PopulationGroup == 1, 'Low', ifelse(PopulationGroup == 2, 'Medium', ifelse(PopulationGroup == 3, 'High', PopulationGroup))),
         UrbanVSRural = ifelse(UrbanVSRural =='R', 'Rural', ifelse(UrbanVSRural == "U", "Urban", UrbanVSRural)),
         MajorityReligion = ifelse(MajorityReligion != "Muslim", "Non-Muslim Majority", ifelse(MajorityReligion == "Muslim", "Muslim Majority", MajorityReligion))) %>% ungroup %>% group_by(district, MajorityReligion, UrbanVSRural, PopulationGroup) %>% 
  summarize(mean(stone)) %>% ungroup %>% 
  dplyr::select("District" = district,
                "Total Stone Pelting Days" = 'mean(stone)',
         "Religion Majority" = MajorityReligion, 
         "Urban vs Rural" = UrbanVSRural, 
         "Population Density" = PopulationGroup) %>% 
  stargazer(type="latex",
            summary = F,
            title = 'Table of Districts (Newspaper+SATP+ACLED Dataset)',
            notes = "For the Newspaper+SATP+ACLED Dataset",
            float = T,
            header= F,
            align = T,
            rownames = FALSE)
```
```{r echo=FALSE, results='asis', warning = F, error = F, message=F}
final %>% 
  group_by(district) %>% 
  mutate(stone = sum(stone)) %>% 
  dplyr::select(stone, MajorityReligion, UrbanVSRural, PopulationGroup) %>% 
  mutate(PopulationGroup = ifelse(PopulationGroup == 1, 'Low', ifelse(PopulationGroup == 2, 'Medium', ifelse(PopulationGroup == 3, 'High', PopulationGroup))),
         UrbanVSRural = ifelse(UrbanVSRural =='R', 'Rural', ifelse(UrbanVSRural == "U", "Urban", UrbanVSRural)),
         MajorityReligion = ifelse(MajorityReligion != "Muslim", "Non-Muslim Majority", ifelse(MajorityReligion == "Muslim", "Muslim Majority", MajorityReligion))) %>% ungroup %>% group_by(district, MajorityReligion, UrbanVSRural, PopulationGroup) %>% 
  summarize(mean(stone)) %>% ungroup %>% 
  dplyr::select("District" = district,
                "Total Stone Pelting Days" = 'mean(stone)',
         "Religion Majority" = MajorityReligion, 
         "Urban vs Rural" = UrbanVSRural, 
         "Population Density" = PopulationGroup) %>% 
  stargazer(type="latex",
            summary = F,
            title = 'Table of Districts (Newspaper+SATP+ACLED Dataset) (Highlighted)',
            notes = "For the Newspaper+SATP+ACLED Dataset",
            float = T,
            header= F,
            align = T,
            rownames = FALSE)
```

\newpage

# 3  
## Table 3.1  
```{r echo=FALSE, results='asis', warning = F, error = F, message=F}


    stargazer::stargazer(as.data.frame(final) %>% 
                dplyr::select(AmountOfPrecipitationInMM, 
                              TemperatureInC, 
                              RamzanDummy1IsRamzan, 
                              onion_price, 
                              Friday, 
                              Demonetization, 
                              KillingBW, 
                              stone),
              type = 'latex', header = F, align = T, digits = 2,
          summary.stat = c("n", "min", "max", "mean", "median", "sd"),
          title = 'Summary Statistics of the SATP-Newspapers-ACLED Dataset',
          covariate.labels = c("Amount of Precipitation (in mm)",
                               "Temperature (in C)",
                               "Ramzan (1=Ramzan, 0 = Not Ramzan)",
                               "Onion Price (in INR)",
                               "Friday (Dummy)",
                               "Demonetization (Dummy)",
                               "Killing of Burhan Wani (Dummy)",
                               "Stone Pelting Day (Dummy, DV)"),
          column.sep.width = "0pt")

  
```

\newpage

```{r echo=FALSE, results='asis', warning = F, error = F, message=F}
districts = unique(final$district)
for(i in seq(1,length(districts),1)){
    stargazer::stargazer(as.data.frame(final) %>% 
                filter(district == districts[i]) %>% 
                dplyr::select(AmountOfPrecipitationInMM, 
                              TemperatureInC, 
                              RamzanDummy1IsRamzan, 
                              onion_price, 
                              Friday, 
                              Demonetization, 
                              KillingBW, 
                              stone),
              type = 'latex', header = F, align = T, digits = 2,
          summary.stat = c("n", "min", "max", "mean", "median", "sd"),
          title = paste('Summary Statistics of the SATP-Newspapers-ACLED Dataset for', districts[i], "district.", sep=' '),
          covariate.labels = c("Amount of Precipitation (in mm)",
                               "Temperature (in C)",
                               "Ramzan (1=Ramzan, 0 = Not Ramzan)",
                               "Onion Price (in INR)",
                               "Friday (Dummy)",
                               "Demonetization (Dummy)",
                               "Killing of Burhan Wani (Dummy)",
                               "Stone Pelting Day (Dummy, DV)"),
          column.sep.width = "0pt",
          notes = "*please note: if onion price min. is 0 and max is 1, then all values are NAs for that district",
          notes.align = "l")
}
  
```

\newpage 

## Table 3.2  
```{r echo=FALSE, results='asis', warning = F, error = F, message=F}
stargazer::stargazer(as.data.frame(final) %>% filter(MajorityReligion == "Muslim") %>% 
                dplyr::select(AmountOfPrecipitationInMM, 
                              TemperatureInC, 
                              RamzanDummy1IsRamzan, 
                              onion_price, 
                              Friday, 
                              Demonetization, 
                              KillingBW, 
                              stone),
                as.data.frame(final) %>% filter(MajorityReligion != "Muslim") %>% 
                dplyr::select(AmountOfPrecipitationInMM, 
                              TemperatureInC, 
                              RamzanDummy1IsRamzan, 
                              onion_price, 
                              Friday, 
                              Demonetization, 
                              KillingBW, 
                              stone),
              type = 'latex', header = F, align = T, digits = 2,
          summary.stat = c("n", "min", "max", "mean", "median", "sd"),
          title = c('Summary Statistics of the SATP-Newspapers-ACLED Dataset (Muslim Districts ONLY)',
                    'Summary Statistics of the SATP-Newspapers-ACLED Dataset (Non-Muslim Districts ONLY)'),
          covariate.labels = c("Amount of Precipitation (in mm)",
                               "Temperature (in C)",
                               "Ramzan (1=Ramzan, 0 = Not Ramzan)",
                               "Onion Price (in INR)",
                               "Friday (Dummy)",
                               "Demonetization (Dummy)",
                               "Killing of Burhan Wani (Dummy)",
                               "Stone Pelting Day (Dummy, DV)"),
          column.sep.width = "0pt")
  

```

\newpage 

## Table 3.3  
```{r echo=FALSE, results='asis', warning = F, error = F, message=F}
stargazer::stargazer(as.data.frame(final) %>% filter(UrbanVSRural == "U") %>% 
                dplyr::select(AmountOfPrecipitationInMM, 
                              TemperatureInC, 
                              RamzanDummy1IsRamzan, 
                              onion_price, 
                              Friday, 
                              Demonetization, 
                              KillingBW, 
                              stone),
                as.data.frame(final) %>% filter(UrbanVSRural == "R") %>% 
                dplyr::select(AmountOfPrecipitationInMM, 
                              TemperatureInC, 
                              RamzanDummy1IsRamzan, 
                              onion_price, 
                              Friday, 
                              Demonetization, 
                              KillingBW, 
                              stone),
              type = 'latex', header = F, align = T, digits = 2,
          summary.stat = c("n", "min", "max", "mean", "median", "sd"),
          title = c('Summary Statistics of the SATP-Newspapers-ACLED Dataset (Urban Districts ONLY)',
                    'Summary Statistics of the SATP-Newspapers-ACLED Dataset (Rural Districts ONLY)'),
          covariate.labels = c("Amount of Precipitation (in mm)",
                               "Temperature (in C)",
                               "Ramzan (1=Ramzan, 0 = Not Ramzan)",
                               "Onion Price (in INR)",
                               "Friday (Dummy)",
                               "Demonetization (Dummy)",
                               "Killing of Burhan Wani (Dummy)",
                               "Stone Pelting Day (Dummy, DV)"),
          column.sep.width = "0pt")

```

\newpage 

## Table 3.4  
```{r echo=FALSE, results='asis', warning = F, error = F, message=F}
stargazer::stargazer(as.data.frame(final) %>% filter(PopulationGroup != 3) %>% 
                dplyr::select(AmountOfPrecipitationInMM, 
                              TemperatureInC, 
                              RamzanDummy1IsRamzan, 
                              onion_price, 
                              Friday, 
                              Demonetization, 
                              KillingBW, 
                              stone),
                as.data.frame(final) %>% filter(PopulationGroup == 3) %>% 
                dplyr::select(AmountOfPrecipitationInMM, 
                              TemperatureInC, 
                              RamzanDummy1IsRamzan, 
                              onion_price, 
                              Friday, 
                              Demonetization, 
                              KillingBW, 
                              stone),
              type = 'latex', header = F, align = T, digits = 2,
          summary.stat = c("n", "min", "max", "mean", "median", "sd"),
          title = c('Summary Statistics of the SATP-Newspapers-ACLED Dataset (Low and Medium Population Density Districts ONLY)',
                    'Summary Statistics of the SATP-Newspapers-ACLED Dataset (High Population Density Districts ONLY)'),
          covariate.labels = c("Amount of Precipitation (in mm)",
                               "Temperature (in C)",
                               "Ramzan (1=Ramzan, 0 = Not Ramzan)",
                               "Onion Price (in INR)",
                               "Friday (Dummy)",
                               "Demonetization (Dummy)",
                               "Killing of Burhan Wani (Dummy)",
                               "Stone Pelting Day (Dummy, DV)"),
          column.sep.width = "0pt")

```

\newpage 

## Table 3.5  
```{r echo=FALSE, results='asis', warning = F, error = F, message=F}
stargazer::stargazer(
                as.data.frame(final) %>% filter(district == 'Srinagar') %>% 
                dplyr::select(AmountOfPrecipitationInMM, 
                              TemperatureInC, 
                              RamzanDummy1IsRamzan, 
                              onion_price, 
                              Friday, 
                              Demonetization, 
                              KillingBW, 
                              stone),
                as.data.frame(final) %>% filter(district == 'Pulwama') %>% 
                dplyr::select(AmountOfPrecipitationInMM, 
                              TemperatureInC, 
                              RamzanDummy1IsRamzan, 
                              onion_price, 
                              Friday, 
                              Demonetization, 
                              KillingBW, 
                              stone),
                as.data.frame(final) %>% filter(district == 'Baramulla') %>% 
                dplyr::select(AmountOfPrecipitationInMM, 
                              TemperatureInC, 
                              RamzanDummy1IsRamzan, 
                              onion_price, 
                              Friday, 
                              Demonetization, 
                              KillingBW, 
                              stone),
              type = 'latex', header = F, align = T, digits = 2,
          summary.stat = c("n", "min", "max", "mean", "median", "sd"),
          title = c(
                    'Summary Statistics of the SATP-Newspapers-ACLED Dataset for Srinagar District',
                    'Summary Statistics of the SATP-Newspapers-ACLED Dataset for Pulwama District',
                    'Summary Statistics of the SATP-Newspapers-ACLED Dataset for Baramulla District'),
          covariate.labels = c("Amount of Precipitation (in mm)",
                               "Temperature (in C)",
                               "Ramzan (1=Ramzan, 0 = Not Ramzan)",
                               "Onion Price (in INR)",
                               "Friday (Dummy)",
                               "Demonetization (Dummy)",
                               "Killing of Burhan Wani (Dummy)",
                               "Stone Pelting (DV)"),
          column.sep.width = "0pt")

```

